﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UNBP.Model;

namespace Portal.Exam.Render
{
    public class AddMultiChoice 
    {
        [AjaxPro.AjaxMethod]
        public string GetHtml(string SiteId)
        {
            string MonthiId = (string)WebEnvironments.Request(SiteId, "MonthiId");
            string BackUrl = "Question.aspx";
            string SelectLevel =
              "<select id=drpSelectLevelQuestion>\r\n" +
              "   <option value=\"0\">Mức độ dễ</option>\r\n" +
              "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
              "   <option value=\"2\">Mức độ khó</option>\r\n" +
              "</select>\r\n";

            string RetHtml =
                " <script language=javascript> \r\n" +
                " function ClientSaveAddNewMultiChoiceQuestion() \r\n"+
                "     { \r\n"+
                "         SiteId=document.getElementById(\"txtSiteId\").value; \r\n"+
                "         oEditor = FCKeditorAPI.GetInstance('FCKeditorBody');\r\n" +
                "         Title= oEditor.GetXHTML( true ); \r\n" +
                "         MaxPoint=document.getElementById(\"txtPoint\").value; \r\n"+
                "         MonthiId=document.getElementById(\"txtMonthiId\").value; \r\n"+
                "         SecondTime = document.getElementById(\"txtSecondTime\").value; \r\n"+
                "         IsMemory = document.getElementById(\"chkIsMemory\").checked; \r\n"+
                "         SecondHidden = document.getElementById(\"txtSecondHidden\").value; \r\n"+
                "         SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                "         SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +
                "         QuestionId=Portal.Exam.AddMultiChoice.ServerSideSaveAddNewMultiChoiceQuestion(SiteId, MonthiId,Title,MaxPoint,SecondTime,IsMemory,SecondHidden,SelectLevel).value; \r\n" +
                "         if(QuestionId.indexOf(\"ERROR\")!=-1) \r\n"+
                "         { \r\n"+
                "             alert(QuestionId); \r\n"+
                "             return; \r\n"+
                "         } \r\n"+
                "         document.getElementById(\"txtQuestion\").value=\"\"; \r\n"+
                "         document.getElementById(\"txtPoint\").value=\"1\"; \r\n"+
                "         document.getElementById(\"txtSecondTime\").value=\"60\"; \r\n"+
                "         document.getElementById(\"txtQuestion\").focus(); \r\n"+
                "         ShowClientUpdateUrlForm(QuestionId); \r\n" +
                "   } \r\n"+

                " function ShowClientUpdateUrlForm(QuestionId) \r\n"+
                " { \r\n"+
                "     SiteId=document.getElementById(\"txtSiteId\").value; \r\n"+
                "     Url=Portal.Exam.AddMultiChoice.GetShowUpdateForm(SiteId, QuestionId).value; \r\n" +
                "     if(Url.indexOf(\"ERROR\")!=-1) \r\n"+
                "     { \r\n"+
                "         alert(Url); \r\n"+
                "         return; \r\n"+
                "     } \r\n"+
                "     window.open(Url, '_self'); \r\n"+
                " } \r\n" +
                " </script>\r\n"+
                "<div class=FormSaveUpdate>\r\n"+
                "      <input type=hidden id=txtMonthiId value=\"" + MonthiId + "\">" +
                "      <table>" +
                "         <tr>" +
                "              <td class=Col1>Mức độ</td>" +
                "              <td class=Col2>" + SelectLevel + "</td>" +
                "         </tr>" +
                "          <tr>" +
                "              <td class=Col1>Câu hỏi:</td>" +
                "              <td class=Col2>\r\n"+
                "               <textarea type=hidden id=\"FCKeditorBody\" name=\"FCKeditorBody\">" + QuestionCls.Question + "</textarea>\r\n" +
                "               <script type=\"text/javascript\" src=\"/fckeditor/fckeditor.js\"></script>\r\n" +
                "               <script type=\"text/javascript\">\r\n" +
                "                   var oFCKeditor = new FCKeditor('FCKeditorBody');\r\n" +
                "                   oFCKeditor.BasePath = \"/fckeditor/\";\r\n" +
                "                   oFCKeditor.Height = \"600\";\r\n" +
                "                   oFCKeditor.ReplaceTextarea();\r\n" +
                "               </script>\r\n" +
                "             </td>\r\n"+
                "          </tr>" +
                "          <tr>" +
                "              <td class=Col1>Điểm:</td>" +
                "              <td class=Col2><input id=txtPoint value=\"1\"></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=Col1>Thời gian(giây):</td>" +
                "              <td class=Col2><input id=txtSecondTime value=\"30\"></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=Col1>Câu hỏi trí nhớ:</td>" +
                "              <td class=Col2><input type=checkbox id=chkIsMemory></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=Col1>Thời gian ẩn:</td>" +
                "              <td class=Col2><input id=txtSecondHidden value=\"30\"></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td></td>" +
                "              <td><input type=button onclick=javascript:ClientSaveAddNewMultiChoiceQuestion(); value=\"Ghi lại\"><input type=button  onclick=\"javascript:window.history.back(-1);\" value=\"Quay lại\"><input type=button  onclick=\"javascript:window.open('" + BackUrl + ",'_self');\" value=\"Danh sách\"></td>" +
                "          </tr>" +
                " </table>"+
                "</div>\r\n";
            return RetHtml;
        }

        public override string RenderWebPart(LoadParamCls oLoadParamCls)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.Exam.AddMultiChoice));
                AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.Exam.Question));
                string SiteId = oLoadParamCls.SiteId;
                string RetHtml = "<div id=divAddBestAnsContent>"+ GetHtml(SiteId)+"</div>";

                return RetHtml;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSaveAddNewMultiChoiceQuestion(string SiteId, string MonthiId, string Title, string MaxPoint, string SecondTime, bool IsMemory, string SecondHidden, string SelectLevel)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                if (Title.Trim().Length == 0)
                {
                    throw new Exception("Nhập tiêu đề cậu hỏi");
                }
                if (FuncUtilities.checkInteger(MaxPoint) == false)
                {
                    throw new Exception("Dữ liệu điểm nhập không hợp lệ! Phải nhập kiểu số!");
                }
                return new QuestionBll().AddMultiChoiceQuestion(null, SiteId, "MultiChoice", MonthiId, Title, int.Parse(MaxPoint), int.Parse(SecondTime),IsMemory,int.Parse(SecondHidden),int.Parse(SelectLevel));
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string SiteId, string QuestionId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                QuestionCls QuestionCls = new QuestionBll().CreateModel(SiteId, QuestionId);

                string UpdateUrl = "";
                if (QuestionCls.Type.ToLower().Equals("yesno"))
                {
                    UpdateUrl = "UpdateYesNo.aspx?QuestionId=QuestionId";
                }
                if (QuestionCls.Type.ToLower().Equals("bestanswer"))
                {
                    UpdateUrl = "UpdateBestAnswer.aspx?QuestionId=QuestionId";
                }
                if (QuestionCls.Type.ToLower().Equals("MultiChoice".ToLower()))
                {
                    UpdateUrl = "UpdateMultiChoice.aspx?QuestionId=QuestionId";
                }
                if (QuestionCls.Type.ToLower().Equals("FreeText".ToLower()))
                {
                    UpdateUrl = "UpdateFreeText.aspx?QuestionId=QuestionId";
                }

                if (QuestionCls.Type.ToLower().Equals("LargeFreeText".ToLower()))
                {
                    UpdateUrl = "UpdateFreeText.aspx?QuestionId="+QuestionId;
                }
                if (UpdateUrl.Trim().Length == 0)
                {
                    throw new Exception("Không cung cấp tính năng sửa chữa cho loại: " + QuestionCls.Type);
                }
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

    }
}